<template>
	<el-container v-loading="loading">
		<el-container>
			<el-header>
				<div class="left-panel">
				</div>
				<div class="right-panel">
					<div class="right-panel-search">
						<el-input v-model="search.patient_sn" placeholder="问诊单号" clearable></el-input>
						<el-input v-model="search.patient_name" placeholder="就诊人" clearable></el-input>
						<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
						<el-button type="info" @click="resetsearch">重置</el-button>
						<el-button type="info" @click="refresh">刷新</el-button>
					</div>
				</div>
			</el-header>
			<el-main class="nopadding">
				<el-alert title="数据更新：每分钟自动更新" type="warning" show-icon :closable="false" style="margin-bottom: 15px;"/>
				<scTable ref="table" :apiObj="apiObj" @selection-change="selectionChange" stripe remoteSort :params="search"
						 remoteFilter>
<!--					<el-table-column type="selection" width="50"></el-table-column>-->
					<el-table-column label="问诊时间" prop="created_at"></el-table-column>
					<el-table-column label="就诊人" prop="patient_name"></el-table-column>
					<el-table-column label="账号" prop="user_mobile"></el-table-column>
					<el-table-column label="问诊单号" prop="patient_sn"></el-table-column>
				</scTable>
			</el-main>
		</el-container>
	</el-container>
</template>

<script>

	export default {
		name: 'user',
		components: {
		},
		data() {
			return {
				timer:null, //定时器名称
				dialog: {
					save: false
				},
				showGroupLoading: false,
				loading: false,
				apiObj: this.$API['patient.patient_order'].index,
				selection: [],
				statusList: [
					{status: 0, status_name: "停用"},
					{status: 1, status_name: "启用"},
				],
				companyList: [],
				search: {
					patient_sn: null,
					patient_name:null,
					is_wait:1,
				},
				//自动刷新使用
				intervalId: null
			}
		},
		mounted() {

		},
		methods: {
			//添加
			add() {
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open()
				})
			},
			//编辑
			handleData(mode, row) {
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open(mode).setData(row)
				})
			},
			//删除
			deleteUser(mode, row) {
				console.log(mode, row);
				this.$confirm(`注：确认删除单位吗？`, '提示', {
					type: 'warning',
					confirmButtonText: '确定',
					confirmButtonClass: 'el-button--danger'
				}).then(() => {
					this.$API['user.company'].deleteUser.post({company_id: row.company_id})
						.then((res) => {
							if (res.code == 0) {
								this.$refs.table.refresh()
								this.$message.success("操作成功")
							} else {
								this.$alert(res.message, "提示", {type: 'error'})
							}
						})
						.catch((msg) => {
							this.$alert(msg, "提示", {type: 'error'})
						})
						.finally(() => {

						});
				}).catch(() => {
				})

			},
			//表格选择后回调事件
			selectionChange(selection) {
				this.selection = selection;
			},
			//表格内开关
			changeSwitch(val, row) {
				row.$switch_status = true;
				this.$API['user.company'].changeStatus.post({status: val, company_id: row.company_id})
					.then((res) => {
						if (res.code == 0) {
							this.$emit('success', this.form, this.mode)
							this.$message.success("操作成功")
							row.status = val;
						} else {
							this.$alert(res.message, "提示", {type: 'error'})
						}
					})
					.catch((msg) => {
						this.$alert(msg, "提示", {type: 'error'})
					})
					.finally(() => {
						row.$switch_status = false;
					});
			},
			//搜索
			upsearch() {
				this.$refs.table.upData(this.search)
			},
			//重置
			resetsearch() {
				this.search = {
					patient_sn: null,
					patient_name:null,
					is_wait:1,
				}
				this.$refs.table.upData(this.search)
			},
			//本地更新数据
			handleSuccess() {
				this.dialog.save = false
				this.$refs.table.reload()
			},
			refresh(){
				this.$refs.table.upData(this.search)
			},
		},
		//自动刷新使用
		created() {
			this.intervalId = setInterval(() => {
				console.log('每一分钟刷新一下');
				this.refresh();
			}, 60000); // 60000毫秒等于1分钟
		},
		/*beforeDestroy() {
			clearInterval(this.intervalId); // 在组件销毁前清除定时器
		},*/
		beforeRouteLeave() {
			// 在离开当前页面之前执行的逻辑
			console.log('离开当前页面');
			// 执行完成后的跳转操作，可以调用next()方法进行跳转
			// next('/new-route'); // 跳转到新路由
			clearInterval(this.intervalId); // 在组件销毁前清除定时器
		}
	}
</script>

<style>
</style>
